package com.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.errors.IOError;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	import flash.utils.*;
	
	public class ImageLoader extends Sprite
	{
		private static var _cacheList:Object = {};
		private var _url:String;
		private var _loader:Loader;
		private var _imageWidth:Number = 0;
		private var _imageHeight:Number = 0;
		private var _callback:Function;
		private var _loadIcon:LoadIcon;
		private var _applicationDomain:ApplicationDomain;
		
		public function ImageLoader(url:String = "", imageWidth:Number = 0, imageHeight:Number = 0, callback:Function = null)
		{
			if(!_loadIcon) _loadIcon = new LoadIcon(this,imageWidth,imageHeight);
			
			_imageWidth = imageWidth;
			_imageHeight = imageHeight;
			_callback = callback;
			
			this.changeUrl(url, callback);
		}
		
		override public function get width():Number { return _imageWidth; }
		override public function get height():Number { return _imageHeight; }
		
		public function get content():MovieClip
		{
			return MovieClip(_loader.content);
		}
		
		public function changeUrl(url:String, callback:Function = null):void
		{
			this._url = url;
			this._callback = callback;
			
			this.loaderProgress();
		}
		
		public function dispose():void
		{
			if(_cacheList[this._url] is MovieClip){
				_cacheList[this._url].stop();
			}
			this.clearThis();
		}
		
		private function loaderProgress():void
		{
			if(this._url == null || this._url == "") return;
			if (_cacheList[this._url] is BitmapData){
				var bitmap:Bitmap = new Bitmap(_cacheList[this._url], "auto", true);
				addChild(bitmap);
				this.onCallback(bitmap.width, bitmap.height);
			}else if (_cacheList[this._url] is Class){
				var displayObject:DisplayObject = new _cacheList[this._url];
				addChild(displayObject);
				this.onCallback(displayObject.width, displayObject.height);
			}else if(_cacheList[this._url] is MovieClip){
				var mc:DisplayObject = _cacheList[this._url];
				addChild(mc);
				this.onCallback(mc.width, mc.height);
			}else{
				this.addChild(_loadIcon);
				if(this._loader == null){
					this._loader = new Loader();
					this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
					this._loader.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onError);
					this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onUriError);
				}
				
				if(this._applicationDomain == null){
					this._applicationDomain = new ApplicationDomain();
				}
				
				this._loader.load(new URLRequest(this._url), new LoaderContext(true,  new ApplicationDomain(ApplicationDomain.currentDomain), Security.sandboxType == "localTrusted"?null: SecurityDomain.currentDomain));
			}
		}
		
		private function onComplete(e:Event):void{
			
			clearThis();
			
			addChild(_loader.content);
			
			if (_loader){
				var tempContent:Object = _loader.content;
				if (tempContent is Bitmap){
					(tempContent as Bitmap).smoothing = true;
					if (!_cacheList[_url]) _cacheList[_url] = (tempContent as Bitmap).bitmapData.clone();
				}
				else if(tempContent is MovieClip)
				{
					var mc:MovieClip = (tempContent as MovieClip).getChildAt(0) as MovieClip;
					if(mc && mc.totalFrames == 1) _cacheList[_url] = tempContent as MovieClip;
				}
				else{
					_cacheList[_url] = tempContent as Class;
				}
			}
			
			this.onCallback(_loader.content.width, _loader.content.height);
		}
		
		private function onCallback(w:int, h:int):void
		{
			if(_imageWidth == 0){
				_imageWidth = w;
			}else{
				this.width  = _imageWidth;
			}
			if(_imageHeight == 0){
				_imageHeight = h;
			}else{
				this.height  = _imageHeight;
			}
			
			if(this._callback is Function) this._callback();
		}
		
		private function onError(e:ErrorEvent):void
		{
			trace("未读取到图片地址")
		}
		
		private function onUriError(e:ErrorEvent):void
		{
			trace("未读取到图片地址"+_url);
		}
		
		
		private function clearThis() : void
		{
			if(_loadIcon){
				if(_loadIcon.parent) _loadIcon.parent.removeChild(_loadIcon);
				_loadIcon = null;
			}
			while (numChildren){
				removeChildAt(0);
			}
		}
		
	}
}